package com.cloud.fast.generate;

import java.io.File;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.toolkit.StringUtils;

/**
 * MySQL 生成演示
 */
public class MpGenerator {

//	private static final String JDBC_URL = "jdbc:mysql://47.94.166.88:3306/book?characterEncoding=utf8&autoReconnect=true&useSSL=false";
//	private static final String JDBC_USERNAME = "root";
//	private static final String JDBC_PASSWORD = "xiaoyu";
//	private static final String JDBC_DIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
//	private static final DbType DB_TYPE = DbType.MYSQL;

	private static String JDBC_URL = "jdbc:postgresql://10.100.254.11:5432/BJC20170727";
	private static String JDBC_USERNAME = "myerp";
	private static String JDBC_PASSWORD = "godblessme";
	private static String JDBC_DIVER_CLASS_NAME = "org.postgresql.Driver";
	private static DbType DB_TYPE = DbType.POSTGRE_SQL;
	
	// 本地项目地址
//	private static final String PROJECT_DIR = "C:\\Users\\Admin\\svn\\wechatOA\\webapp\\WEB-INF\\viewstemp";
	private static String PROJECT_DIR = "C:\\Users\\Admin\\git\\cloud-test1\\";
	// 模块名
	private static String MODULE_NAME = "demo1";
	private static String OUTPUT_API_DIR;
//	private static String OUTPUT_API_DIR = PROJECT_DIR + MODULE_NAME + "-api\\src\\main\\java";
//	private static String OUTPUT_IMPL_DIR = PROJECT_DIR + MODULE_NAME + "-impl\\src\\main\\java";
	private static String DAO_XML_DIR;
//	private static String DAO_XML_DIR = PROJECT_DIR + MODULE_NAME + "-impl\\src\\main\\resources\\mapper";

	// 包名开头
	private static String BASE_PACKAGE = "com.bjc";
//	// 模块名称
	private static String MODULE_PACKAGE = "demo1";
//	// 表名前缀，多个以,分隔
	private static String[] TABLE_PREFIX = {"bjc_tj_"};
	private static String AUTHOR = "xuzihui";

	
	public static void BeginGenerator(BeginGenerator args) {
		JDBC_URL = args.getJDBC_URL();
		JDBC_USERNAME = args.getJDBC_USERNAME();
		JDBC_PASSWORD = args.getJDBC_PASSWORD();
		JDBC_DIVER_CLASS_NAME = args.getJDBC_DIVER_CLASS_NAME();
		DB_TYPE = args.getDB_TYPE();
		
		PROJECT_DIR = args.getPROJECT_DIR();
		MODULE_NAME =args.getMODULE_NAME();
		MODULE_PACKAGE = args.getMODULE_PACKAGE();
		TABLE_PREFIX = args.getTABLE_PREFIX();
		AUTHOR = args.getAUTHOR();
		OUTPUT_API_DIR = PROJECT_DIR + MODULE_NAME + "-api\\src\\main\\java";
		DAO_XML_DIR = PROJECT_DIR + MODULE_NAME + "-impl\\src\\main\\resources\\mapper";
		
		genCodeApi(args.getTABLE_NAMES());
	}

	private static void genCodeApi(String... tableNames) {
		AutoGenerator mpg = new AutoGenerator();
		// 全局配置
		GlobalConfig gc = new GlobalConfig().setOutputDir(OUTPUT_API_DIR).setFileOverride(true).setActiveRecord(true)// 不需要ActiveRecord特性的请改为false
				.setActiveRecord(false)// 领域模型

				.setEnableCache(false)// XML 二级缓存
				.setBaseResultMap(true)// XML ResultMap
				.setBaseColumnList(true)// XML columList
				.setAuthor(AUTHOR).setMapperName("I%sDao").setXmlName("I%sDao").setServiceName("I%sService")
				.setServiceImplName("%sImpl").setControllerName("%sController");
		mpg.setGlobalConfig(gc);

		// 包配置
		PackageConfig pc = new PackageConfig().setParent(BASE_PACKAGE)
				.setMapper("dao")
				.setEntity("bean")
				.setModuleName(MODULE_PACKAGE);
		mpg.setPackageInfo(pc);

		// 数据源配置
		DataSourceConfig dsc = new DataSourceConfig().setDbType(DB_TYPE).setDriverName(JDBC_DIVER_CLASS_NAME)
				.setUsername(JDBC_USERNAME).setPassword(JDBC_PASSWORD).setUrl(JDBC_URL);
		mpg.setDataSource(dsc);

		// 策略配置
		StrategyConfig strategy = new StrategyConfig();
		strategy.setTablePrefix(TABLE_PREFIX);// 此处可以修改为您的表前缀
		strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
		strategy.setInclude(tableNames); // 需要生成的表
		strategy.setEntityBuilderModel(true);
		strategy.setEntityLombokModel(true);
		
		// strategy.setExclude(new String[]{"test"}); // 排除生成的表
//		strategy.setSuperEntityClass(BASE_PACKAGE + "." + MODULE_PACKAGE + "." + "bean.response");
		// 自定义 dao 父类
//		strategy.setSuperapperClass("com.bjc.base.bean.BaseVO");
		mpg.setStrategy(strategy);

		// 注入自定义配置，可以在 VM 中使用 cfg.abc 【可无】 实例： #{cfg.abc} 可取下面abc的设置的值
		InjectionConfig cfg = new InjectionConfig() {
			@Override
			public void initMap() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
				map.put("date", LocalDate.now());
				this.setMap(map);
			}
		};

		// 自定义 requestVO生成
		List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
		focList.add(new FileOutConfig("entityReqVO.java.vm") {
			@Override
			public String outputFile(TableInfo tableInfo) {
				return OUTPUT_API_DIR + File.separator + BASE_PACKAGE.replace(".", "\\") + File.separator + MODULE_PACKAGE.replace(".", "\\") + "\\bean\\request"
						+ File.separator + tableInfo.getEntityName() + "ReqVO.java";
			}
		});
		
		// 自定义 requestVO生成
		focList.add(new FileOutConfig("entityVO.java.vm") {
			@Override
			public String outputFile(TableInfo tableInfo) {
				return OUTPUT_API_DIR + File.separator + BASE_PACKAGE.replace(".", "\\") + File.separator + MODULE_PACKAGE.replace(".", "\\") + "\\bean\\response"
						+ File.separator + tableInfo.getEntityName() + "VO.java";
			}
		});
		
//		// 调整 controller 生成目录演示
//		focList.add(new FileOutConfig("controller.java.vm") {
//			@Override
//			public String outputFile(TableInfo tableInfo) {
//				return OUTPUT_IMPL_DIR + File.separator + MODULE_PACKAGE + File.separator + "I" + tableInfo.getEntityName() + "Dao.xml";
//			}
//		});
//		// 调整 impl 生成目录演示
//		focList.add(new FileOutConfig("serviceImpl.java.vm") {
//			@Override
//			public String outputFile(TableInfo tableInfo) {
//				return OUTPUT_IMPL_DIR + File.separator + MODULE_PACKAGE + File.separator + "I" + tableInfo.getEntityName() + "Dao.xml";
//			}
//		});
		
		// 调整 xml 生成目录演示
		focList.add(new FileOutConfig("dao.xml.vm") {
			@Override
			public String outputFile(TableInfo tableInfo) {
				return DAO_XML_DIR + File.separator + MODULE_PACKAGE + File.separator + "I" + tableInfo.getEntityName() + "Dao.xml";
			}
		});
		
		cfg.setFileOutConfigList(focList);
		mpg.setCfg(cfg);

		TemplateConfig tc = new TemplateConfig().
				setController("controller.java.vm").
				setEntity(StringUtils.isNotEmpty(OUTPUT_API_DIR) ? null :"entityVO.java.vm")
				.setMapper("dao.java.vm").
				setXml(StringUtils.isNotEmpty(DAO_XML_DIR) ? null : "dao.xml.vm")
				.setService("service.java.vm").setServiceImpl("serviceImpl.java.vm");
		mpg.setTemplate(tc);

		// 执行生成
		mpg.execute();
	}

}
